#include"game.h"
int x=0,y=0;
void Menu()
{
    printf("#################################\n");
    printf("######  1.begin   0.exit   ######\n");
    printf("#################################\n");
}
//按照特定的方向求总数
int ChessCount(int board[][COL],int row,int col,enum Dir dirction)
{
    int _x=x-1;
    int _y=y-1;
    int count=0;
    while(1)
    {
        switch(dirction)
        {
            case LEFT:
                _y--;
                break;
            case RIGHT:
                _y++;
                break;
            case UP:
                _x++;
                break;
            case DOWN:
                _x--;
                break;
            case LEFT_UP:
                _y--;
                _x++;
                break;
            case LEFT_DOWN:
                _y--;
                _x--;
                break;
            case RIGHT_UP:
                _y++;
                _x++;
                break;
            case RIGHT_DOWN:
                _y++;
                _x--;
                break;
            default:
                break;
        }
        if(_x<0||_x>row-1||_y<0||_y>col-1)
        {
            break;
        }
        if(board[x-1][y-1]==board[_x][_y])
        {
            count++;
        }
        else {
            break;
        }
    }
    return count;

}
int IsOver(int board[][COL],int row,int col)
{
    int count1=ChessCount(board,row,col,LEFT)+ChessCount(board,row,col,RIGHT)+1;
    int count2=ChessCount(board,row,col,UP)+ChessCount(board,row,col,DOWN)+1;
    int count3=ChessCount(board,row,col,LEFT_UP)+ChessCount(board,row,col,LEFT_DOWN)+1;
    int count4=ChessCount(board,row,col,RIGHT_UP)+ChessCount(board,row,col,RIGHT_DOWN)+1;
    if(count1>=5||count2>=5||count3>=5||count4>=5)
    {
        return board[x-1][y-1];
    }
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            if(board[i][j]==0)
            {
                return NEXT;
            }
        }
    }
    return DRAW;
}
void PlayerMove(int board[][COL],int row,int col,int play)
{
    while(1)
    {
        printf("Player[%d] Please enter your pos\n",play);
        scanf("%d %d",&x,&y);
        if(x<1||x>row||y<1||y>row)
        {
            printf("pos is not right\n");
            continue;
        }
        if(board[x-1][y-1]==0)
        {
            board[x-1][y-1]=play;
            break;
        }
        else    
        {
            printf("pos is not right\n");
            continue;
        }

    }
}
void ShowBoard(int board[][COL],int row,int col)
{
    printf("\e[1;1H\e[2J");
    printf(" ");
    for(int i=1;i<=col;i++)
        printf("%3d",i);
    printf("\n");
    for(int i=0;i<row;i++)
        {
            printf("%2d",i+1);
            for(int j=0;j<row;j++)
            {
                if(board[i][j]==0)
                {
                    printf(" . ");
                }
                else if(board[i][j]==player1)
                {
                    printf(" ○ ");
                }
                else if(board[i][j]==player2)
                {
                    printf(" ● ");
                }
            }
            printf("\n");
        }
}
void Game()
{
    int board[ROW][COL];
    memset(board,0,sizeof(board));
    int result=NEXT;
    do{
        ShowBoard(board,ROW,COL);
        PlayerMove(board,ROW,COL,player1);
        result=IsOver(board,ROW,COL);
        if(NEXT!=result)
        {
             break;
        }
        ShowBoard(board,ROW,COL);
        PlayerMove(board,ROW,COL,player2);
        result=IsOver(board,ROW,COL);
        if(NEXT!=result)
        {
            break;
        }
    }while(1);
    switch(result)
    {
        case PLAY1_WIN:
            printf("PLAY1_WIN\n");
            ShowBoard(board, ROW, COL);
            break;
        case PLAY2_WIN:
            printf("PLAY2_WIN\n");
            ShowBoard(board, ROW, COL);
            break;
        
        case DRAW:
            printf("DRAW\n");
            ShowBoard(board, ROW, COL);
            break;
    }
}